package com.zx.dsptuneup_TriMic.Util;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.zx.dsptuneup_TriMic.Adapter.AddDeviceAdapter;
import com.zx.dsptuneup_TriMic.Adapter.EquipListAdapter;
import com.zx.dsptuneup_TriMic.R;
import com.zx.dsptuneup_TriMic.bean.DeviceBean;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class AddDeviceDialogUtil {
    private static List<DeviceBean> mdeviceList = null;   // 设备列表
    private static AddDeviceAdapter addDeviceAdapter;
    private static Dialog confirmDialog;

    private static Drawable drawable_selected = null;
    private static Drawable drawable_normal = null;

    public static void showAlertDialog(Activity context, String title,List<DeviceBean> dataList, boolean canceledOnTouchOutside, final ConfirmDialogListener confirmDialogListener) {
        if (context == null || context.isFinishing()) {
            return;
        }
        if(dataList != null && dataList.size() != 0){
            mdeviceList =new ArrayList(Arrays.asList(new Object[dataList.size()]));
            Collections.copy(mdeviceList,dataList);
        }
        initDialog();

        confirmDialog = new Dialog(context, R.style.addDevice_dialog);
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_add_devices, null);
        confirmDialog.setContentView(view);
        confirmDialog.setCancelable(canceledOnTouchOutside);
        Button confirm = view.findViewById(R.id.confir_btn);

        ListView contentListView = view.findViewById(R.id.listView);
        TextView selAllTxt = view.findViewById(R.id.selAllTxt);      // 全选

        // 关闭按钮
        Button close = (Button) view.findViewById(R.id.closeBtn);
        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (confirmDialogListener != null) {
                    confirmDialogListener.onClickButton(false, true,null);
                }
                if (confirmDialog != null && confirmDialog.isShowing()) {
                    confirmDialog.dismiss();
                }
            }
        });

        Rect bonds = new Rect(0, 0, 30,30);
        drawable_selected = context.getResources().getDrawable(R.drawable.add_check_sel);
        drawable_selected.setBounds(bonds);

        drawable_normal = context.getResources().getDrawable(R.drawable.add_check_nor);
        drawable_normal.setBounds(bonds);

        //  全选按钮点击逻辑
        selAllTxt.setSelected(false);
        selAllTxt.setCompoundDrawables(drawable_normal, null, null, null);
        selAllTxt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                selAllTxt.setSelected(!selAllTxt.isSelected());
                if(selAllTxt.isSelected()){
                    selAllTxt.setCompoundDrawables(drawable_selected, null, null, null);
                    addDeviceAdapter.sellectAll();
                }else{
                    selAllTxt.setCompoundDrawables(drawable_normal, null, null, null);
                    addDeviceAdapter.cancelSelectAll();
                }
            }
        });

        addDeviceAdapter = new AddDeviceAdapter(context, mdeviceList);
        addDeviceAdapter.setGridViewBeanList(mdeviceList);
        contentListView.setAdapter(addDeviceAdapter);


//        confirmDialog = builder.create();
        confirmDialog.show();

        WindowManager windowManager = context.getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        WindowManager.LayoutParams lp = confirmDialog.getWindow().getAttributes();
        lp.width = (int) (display.getWidth() * 0.85); //设置宽度
        lp.height = (int) (display.getHeight() * 0.8);

        confirmDialog.getWindow().setAttributes(lp);
        confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (confirmDialogListener != null) {
                    confirmDialogListener.onClickButton(true,false, addDeviceAdapter.getSelectedItem());
                }
                if (confirmDialog != null && confirmDialog.isShowing()) {
                    confirmDialog.dismiss();
                }
            }
        });

        TextView title_tv = (TextView) view.findViewById(R.id.title_tv);
        if(title != null && !TextUtils.isEmpty(title)){
            title_tv.setText(title);

        }
    }

    /**
     * 新的對話框彈出之前，先把舊的清除掉
     */
    private static void initDialog() {
        if (confirmDialog != null && confirmDialog.isShowing()) {
            confirmDialog.dismiss();
            confirmDialog = null;
        }
    }

    public interface ConfirmDialogListener {
        void onClickButton(boolean clickConfirm,boolean clickCancel, List<DeviceBean> list);
    }

}
